//
// Notepad.m
// Copyright (c) 1990 by Jiro Nakamura 
// All rights reserved
//
// Handles a notepad, opens it, lets the user modify it,
//  and if anything changes, lets her or him save it before
// closing it.
//
//	by Jiro Nakamura (ac6y@vax5.cit.cornell.edu)
//
// RCS Information
// Revision Number->	$Revision: 2.6 $
// Last Revised->		$Date: 90/10/27 17:53:35 $
//
static char rcsid[] = "$Id: Notepad.m,v 2.6 90/10/27 17:53:35 jiro Exp Locker: jiro $";


#import "Notepad.h"
#import "Global.h"
#import "cass.h"		// for NOTEPADICON
#import <appkit/Form.h>		// for Forms
#import <appkit/TextField.h>
#import <strings.h>		// for strcpy

// The minimum dimensions of the notepad window
#define MIN_WIDTH			125.0
#define MIN_HEIGHT			100.0


@implementation Notepad
+ new
{
	pageNumber = 1;
	self = [super new];
	[self	setDelegate:self];
	return self;
}

- open:sender
{
	static int oldPageNumber = 0;
	static char notepadFile[FILENAME_BUFFER], temp[FILENAME_BUFFER];
	static BOOL alreadyInited = FALSE;
	
	
	if( !alreadyInited)
		{
		alreadyInited = TRUE;
		[self placeWindow: [global notepadFrame]];
		[self	setTitle: 	"Notepad"];
		[self 	setWindowIcon: 	NOTEPADICON];
		}
		
	if( pageNumber < 1)
		pageNumber = 1;
		
	strcpy( notepadFile, [global notepadFile]);

	 sprintf(temp, "%s.%d", notepadFile, pageNumber);
	 strcpy(notepadFile, temp);

	#ifdef DEBUG
		fprintf(stderr,"Opening notepad on page %d.\n"
				"File opened is <%s>\n", 
				pageNumber, notepadFile);
	#endif
		
	[pageNumberForm		setIntValue: pageNumber at:0];
	[filenameTextField	setStringValue: notepadFile];
	[self openWith: 	notepadFile];
	
	if( oldPageNumber != pageNumber)
		{
		[self update];
		oldPageNumber = pageNumber;
		}
	return self;
}

- close
{
	[global	saveThisWindowPosition:	DEFAULTNOTEPADFRAME : self];
	[super close];
	return self;
}

- pageBackward: sender
{
	pageNumber --;
	if(	[self	isDocEdited])
		[self save];
	[self open: self];
	return self;
}

- pageForward: sender
{
	pageNumber ++;
	if(	[self	isDocEdited])
		[self save];
	[self open: self];
	return self;
}

- pageFormModified: sender
{
	pageNumber = [pageNumberForm intValueAt: 0];
	if(	[self	isDocEdited])
		[self save];
	[self open: self];
	return self;
}
	
	
- windowWillResize: (id) sender toSize: (NXSize *) size
{
	#ifdef DEBUG
		fprintf(stderr,"Window would have resized to %f x %f.\n",
				size->width, size->height);
	#endif
	
	if( size->width < MIN_WIDTH)
		size->width = MIN_WIDTH;
	if( size->height < MIN_HEIGHT)
		size->height = MIN_HEIGHT;
		
	#ifdef DEBUG
		fprintf(stderr,"Window will resize to %f x %f.\n", size->width, size->height);
	#endif
	return self;
}
@end
